import * as types from '../constants/ActionTypes';
import Request from '../utils/request';

/**
*重点概览，客户偏好，每日趋势
*/
const fetchImportOVerview = isLoading => (dispatch) => {
  dispatch(fetchLoading(isLoading));
  Request.get({
    url: '/dealer/data/overview',
    success: (data) => {
      dispatch(importOVerviewDatas(data));
    },
    fail: () => {
      console.log('fail');
      dispatch(importOVerviewDatas(''));
    },
    error: () => {
      console.log('error');
      dispatch(importOVerviewDatas(''));
    },
  });
};

// 总览分析页面中的选择客户渠道和车系接口
const fetchOVerviewChannel = (carId, channel, cb) => (dispatch) => {
  Request.get({
    url: `/dealer/car/preference?channel=${channel}&car_id=${carId}`,
    success: (data) => {
      dispatch(OVerviewDataChannel(data));
      if (cb) {
        cb({
          result: data.carname,
        });
      }
    },
    fail: () => {
      console.log('fail');
      dispatch(OVerviewDataChannel(''));
      if (cb) {
        cb({
          err: '请求失败',
        });
      }
    },
    error: (err) => {
      console.log('error', err);
      dispatch(OVerviewDataChannel(''));
      if (cb) {
        cb({
          err: '服务器开小差了，请稍后再试',
        });
      }
    },
  });
};


// 每日趋势条件接口（客户渠道，车系，日期）
const fetchEveryDay = (startTime, endTime, ol, all, app,
 vr, led, h5, web, drive, carId) => (dispatch) => {
   console.log('url=', `/dealer/trend/day?day=${startTime}&end_day=${endTime}&ol=${ol}&all=${all}`
    + `&app=${app}&vr=${vr}&led=${led}&h5=${h5}&web=${web}&drive=${drive}&car_id=${carId}`);
   Request.get({
     url: `/dealer/trend/day?day=${startTime}&end_day=${endTime}&ol=${ol}&all=${all}&` +
     `app=${app}&vr=${vr}&led=${led}&h5=${h5}&web=${web}&drive=${drive}&car_id=${carId}`,
     success: (data) => {
       console.log('返回=日期==', data);
       dispatch(EveryDay(data));
     },
     fail: () => {
       console.log('fail');
       dispatch(EveryDay(''));
     },
     error: () => {
       console.log('error');
       dispatch(EveryDay(''));
     },
   });
 };

// 数据模块全部数据
const importOVerviewDatas = importOVerviewData => ({
  type: types.IMPROT_OVERVIEW,
  importOVerviewData,
});

// 总览分析客户渠道
const OVerviewDataChannel = OverviewAnalyChannel => ({
  type: types.OVERVIEW_CHANNEL,
  OverviewAnalyChannel,
});

// 每日趋势
const EveryDay = EveryDayData => ({
  type: types.EVERY_DAY,
  EveryDayData,
});

const fetchLoading = importLoading => ({
  type: types.IMPORT_VIEW_LOADING,
  importLoading,
});

// 每日趋势日期筛选
const fetchFilterTime = (startTime, endTime) => ({
  type: types.EVERY_DAY_TIME,
  startTime,
  endTime,
});

export {
  fetchImportOVerview,
  fetchOVerviewChannel,
  fetchEveryDay,
  fetchFilterTime,
};
